package com.yjd.bi.etl.comm;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.JdbcTemplate;
import com.yjd.bi.report.model.BiDatabaseInfoModel;
import com.yjd.bi.report.service.IBiDatabaseInfoService;
import com.yjd.bi.report.util.BiUtil;
import com.yjd.bi.report.util.BiUtil.ITransExecute;
import com.yjd.comm.annotation.ServiceRemoteProxy;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.rpm.RpmServiceKeyEnum;
import com.yjd.comm.util.FrameUtil;

/**
 * 业务处理父类
 * @author yjd-wap
 *
 */
public class BaseService {
	
	protected Logger logger = Logger.getLogger(this.getClass());


	/**
	 * 数据库信息服务
	 */
	@ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
	private IBiDatabaseInfoService biDatabaseInfoService;
	
	/**
	 * 获取默认的数据源信息
	 * @return
	 * @throws Exception
	 */
	public BiDatabaseInfoModel getBiDatabaseInfoModel(Integer ...bs_type)throws Exception{
		int ind=2;
		for(Integer i:bs_type){
			if(i!=null){
			ind=i;
			}
		}
		return this.biDatabaseInfoService.getModelOne(FrameUtil.newHashMap("bs_type", ind),
				DbWREnums.READ);
	}

	
	 /**
	  * 批量处理数据量
	  * @param model
	  * @param sql
	  * @param dataList
	  * @return
	  * @throws Exception
	  */
	public Integer batchUpdateData(BiDatabaseInfoModel model,final String sql,final List dataList){

		 try{
		  	  return BiUtil.execute(model,new ITransExecute<Integer>() {
	
					@Override
					public Integer execute(JdbcTemplate jts) throws Exception {
						if(dataList.size()>0){
							return jts.batchUpdate(sql, dataList).length;
						}
						return null;
							
					}
					
				});
			}catch(Exception e){
				logger.info("批量处理插入失败，处理sql:"+sql+"处理数据:"+dataList+"错误信息:"+e);
				return 0;
			}
	  
		}
}
